View Javadoc
1 /* 2 * Scope: a generic MVC framework. 3 * Copyright (c) 2000-2002, The Scope team 4 * All rights reserved. 5 * 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * Neither the name "Scope" nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * 36 * $Id: SComboBoxModel.java,v 1.10 2002/09/12 18:24:41 ludovicc Exp $ 37 */ 38 package org.scopemvc.view.swing; 39 40 41 import javax.swing.ComboBoxModel; 42 import javax.swing.JComboBox; 43 import org.apache.commons.logging.Log; 44 import org.apache.commons.logging.LogFactory; 45 import org.scopemvc.core.Selector; 46 import org.scopemvc.view.util.ModelBindable; 47 48 /*** 49 * @author <A HREF="mailto:daniel.michalik@autel.cz">Daniel Michalik</A> 50 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A> 51 * @created 05 September 2002 52 * @version $Revision: 1.10 $ $Date: 2002/09/12 18:24:41 $ 53 * @see SComboBox 54 */ 55 public final class SComboBoxModel extends SAbstractListModel 56 implements ComboBoxModel { 57 58 private static final Log LOG = LogFactory.getLog(SComboBoxModel.class); 59 60 private boolean _jdk1_3fixIgnoreSetSelectedItem = false; 61 62 private SelectionBoundModel selectionDelegate = new SelectionBoundModel(); 63 private SwingBoundModel selectionBoundModel = new SwingBoundModel(selectionDelegate); 64 65 /*** 66 * Owning combo. 67 */ 68 private JComboBox combo; 69 70 /*** 71 * Need to hold this here in case the model decides to veto some values: UI 72 * can hold invalid values whereas model can't. 73 */ 74 private Object selectedItem; 75 76 77 /*** 78 * Constructor for the SComboBoxModel object 79 * 80 * @param inCombo TODO: Describe the Parameter 81 */ 82 public SComboBoxModel(JComboBox inCombo) { 83 combo = inCombo; 84 } 85 86 87 // ------------------- Delegate to BoundModel ------------------- 88 89 /*** 90 * Gets the bound selection model 91 * 92 * @return The boundSelectionModel value 93 */ 94 public final Object getBoundSelectionModel() { 95 return selectionBoundModel.getBoundModel(); 96 } 97 98 99 /*** 100 * Gets the selection selector 101 * 102 * @return The selectionSelector value 103 */ 104 public final Selector getSelectionSelector() { 105 return selectionBoundModel.getSelector(); 106 } 107 108 109 /*** 110 * Gets the selected item 111 * 112 * @return The selectedItem value 113 */ 114 public Object getSelectedItem() { 115 return selectedItem; 116 } 117 118 119 /*** 120 * Sets the bound selection model 121 * 122 * @param inModel The new boundSelectionModel value 123 */ 124 public final void setBoundSelectionModel(Object inModel) { 125 selectionBoundModel.setBoundModel(inModel); 126 } 127 128 129 /*** 130 * Sets the selection selector 131 * 132 * @param inSelector The new selectionSelector value 133 */ 134 public final void setSelectionSelector(Selector inSelector) { 135 selectionBoundModel.setSelector(inSelector); 136 } 137 138 139 /*** 140 * Sets the selection selector string 141 * 142 * @param inSelectorString The new selectionSelectorString value 143 */ 144 public final void setSelectionSelectorString(String inSelectorString) { 145 if (LOG.isDebugEnabled()) { 146 LOG.debug("setSelectionSelectorString: " + inSelectorString); 147 } 148 selectionBoundModel.setSelectorString(inSelectorString); 149 } 150 151 152 // ------------- View to Model --------------------- 153 154 /*** 155 * Sets the selected item 156 * 157 * @param inItem The new selectedItem value 158 */ 159 public void setSelectedItem(Object inItem) { 160 if (LOG.isDebugEnabled()) { 161 LOG.debug("setSelectedItem: " + inItem); 162 } 163 164 if (_jdk1_3fixIgnoreSetSelectedItem) { 165 return; 166 } 167 168 if ((selectedItem != null && !selectedItem.equals(inItem)) || 169 selectedItem == null && inItem != null) { 170 selectedItem = inItem; 171 selectionBoundModel.updateModel(); 172 } 173 } 174 175 176 // --------------Override for selection model ---------------- 177 178 /*** 179 * Sets the bound model 180 * 181 * @param inModel The new boundModel value 182 */ 183 public void setBoundModel(Object inModel) { 184 super.setBoundModel(inModel); 185 setBoundSelectionModel(inModel); 186 } 187 188 189 // ------------------ Refreshable ------------------------- 190 191 /*** 192 * TODO: document the method 193 */ 194 public void refresh() { 195 super.refresh(); 196 Object propertyValue = selectionBoundModel.getPropertyValue(); 197 boolean propertyReadOnly = selectionBoundModel.getPropertyReadOnly(); 198 selectionDelegate.updateFromProperty(propertyValue, propertyReadOnly); 199 } 200 201 202 /*** 203 * This method is bug fix for JDK 1.2.x,1.3.x bug: after setting model 204 * combobox sets element 0 as selected. This is violation of MVC paradigm - 205 * model describes some state and should not be modified by view. JDK 1.4 206 * beta has this bug fixed. <p> 207 * 208 * Model should be instance of SComboBoxModel to fix take effect</p> . 209 * 210 * @param inIgnore TODO: Describe the Parameter 211 * @see SComboBox.setModel(Object) 212 */ 213 void jdk1_3fixIgnoreSetSelectedItem(boolean inIgnore) { 214 _jdk1_3fixIgnoreSetSelectedItem = inIgnore; 215 } 216 217 218 // ----------------------- Inner class to handle model binding --------------------- 219 220 /*** 221 * TODO: document the method 222 */ 223 void fireContentsChanged() { 224 fireContentsChanged(this, -1, -1); 225 } 226 227 228 class SelectionBoundModel implements ModelBindable { 229 230 /*** 231 * Gets the view value 232 * 233 * @return The viewValue value 234 */ 235 public Object getViewValue() { 236 if (LOG.isDebugEnabled()) { 237 LOG.debug("getValue: " + getSelectedItem()); 238 } 239 240 return getSelectedItem(); 241 } 242 243 244 /*** 245 * TODO: document the method 246 * 247 * @param inValue TODO: Describe the Parameter 248 * @param inReadOnly TODO: Describe the Parameter 249 */ 250 public void updateFromProperty(Object inValue, boolean inReadOnly) { 251 if (LOG.isDebugEnabled()) { 252 LOG.debug("updateFromProperty: " + inValue + ", " + inReadOnly); 253 } 254 255 combo.setEnabled(!inReadOnly); 256 257 if (inValue == selectedItem) { 258 return; 259 } 260 261 // ignore the update when the selection selector is not defined 262 if (getSelectionSelector() == null) { 263 return; 264 } 265 266 if (!combo.isEditable()) { 267 // ensures that the selected item belongs to the list of items 268 if (!containsElement(inValue)) { 269 setSelectedItem(null); 270 throw new RuntimeException("Value " + inValue + " is not contained in the list of items"); 271 } 272 } 273 274 setSelectedItem(inValue); 275 fireContentsChanged(); 276 } 277 278 279 /*** 280 * Validation of the selected item failed 281 * 282 * @param inException The validation failure 283 */ 284 public void validationFailed(Exception inException) { 285 // clear the invalid selected item 286 setSelectedItem(null); 287 if (combo instanceof SComboBox) { 288 ((SComboBox) combo).validationFailed(inException); 289 } 290 } 291 292 293 /*** 294 * Validation of the selected item succeeded 295 */ 296 public void validationSuccess() { 297 if (combo instanceof SComboBox) { 298 ((SComboBox) combo).validationSuccess(); 299 } 300 } 301 } 302 }

This page was automatically generated by Maven